use "$out/emp_info_around_accidents_bnoSext_2022Sept.dta", clear
unique anon 
unique anon if cluster=="" 

* dealing with clustered shocks *
sort anon t
egen seq=seq() if cluster!="" & m==0, by(anon st)
egen mint_clu = min(t) if m==0, by(anon) // start of the (clustered) shock
egen maxt_clue = max(t) if m==0, by(anon) // end of the (clustered) shock
		foreach var of varlist mint_clu maxt_clue {
			egen temp`var'=max(`var'), by(anon)
			drop `var'
			rename temp`var' `var'
		}
* construct EMP (employment) *
gen     EMP = 1 if (vallazon1!=.) & ((w1!=. & w1>0) | (wh1!=. & wh1>0)) & (sick_leave_days<6 | sick_leave_days==.) & (fogvisz1==201)
	
xtset anon t

unique anon 
* keeping those who worked either in the month of the shock or a month before
* for clustered shocks: worked in the 1st month of the clustered shock, or one month before *
egen EMPm0 = mean(EMP) if m==0  & t==mint_clu, by(anon) 
egen EMPml1 = mean(EMP) if m==-1 & t==mint_clu-1, by(anon)
* keep all anons (and all their observations) for whom either Wml1==1 | Wm0==1
foreach var of varlist EMPm0 EMPml1 {
	egen max`var' = max(`var'), by(anon)
	}
	
keep if (maxEMPm0 == 1) | (maxEMPml1 == 1 & maxEMPm0 == .)
gen flagEMPm0 = 1 if maxEMPm0==1
gen flagEMPml1 = 1 if maxEMPml1 == 1 & maxEMPm0 == .
unique anon 
		
* get the first month of EMP after the shock
egen firstEMPtemp = min(m) if m>0 & EMP==1, by(anon)	
egen firstEMP = max(firstEMPtemp), by(anon)
drop firstEMPtemp

gen ABS = 1 if firstEMP>=3 & firstEMP<=12
	unique anon if ABS==1 
	gen ABSd=firstEMP if ABS==1
	
replace ABS = 0 if firstEMP==1 | firstEMP==2
	unique anon if ABS==0 
	* for these, choose d randomly *
	set seed 12345
	gen randomd = runiform(3,13)
	gen dtemp = int(randomd)
		replace ABSd=dtemp if ABS==0
	
keep if ABS!=.

keep if m==0 & t==mint_clu
rename sev ev
keep anon t ev mint maxt ABS ABSd flag*

sum
save "$out/IVdata_HS1_accidents_bnoSext2_SL5_2022Sept26.dta", replace
